package com.yyk.yiliao.ui.mine.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.bean.CardWei_Info;
import com.yyk.yiliao.util.OkHttp3Utlis;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.ValidateUtil;
import com.yyk.yiliao.util.rx.ApiService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 身份验证
 */
public class Identity_Activity extends BaseActivity {

    @BindView(R.id.mIdentity_next)
    Button mIdentityNext;
    @BindView(R.id.b_real_name)
    EditText bRealName;
    @BindView(R.id.b_id_card)
    EditText bIdCard;
    public static Identity_Activity intascne;


    @Override
    public int getLayoutId() {
        return R.layout.activity_identity;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("身份认证");
    }

    @Override
    public void initData() {
        intascne = this;

    }

    @OnClick(R.id.mIdentity_next)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mIdentity_next:

                String bidcard = bIdCard.getText().toString().trim();
                String brealname = bRealName.getText().toString().trim();
                if (!ValidateUtil.isUserName(brealname)) {
                    ToastUtil.showShort(Identity_Activity.this, "请输入正确的姓名");
                    return;
                }
                if (!ValidateUtil.isIDCard(bidcard)) {
                    ToastUtil.showShort(Identity_Activity.this, "请输入正确的身份证号码");
                    return;
                }
                if (!TextUtils.isEmpty(bidcard) && !TextUtils.isEmpty(brealname)) {
                    //当点击按钮的时候就把这些保存下来

                    int uid = Hawk.get("uid");
                    Map<String, String> map = new HashMap<>();
                    map.put("uid", String.valueOf(uid));
                    map.put("real_name", brealname);
                    map.put("id_card", bidcard);
                    Logger.e("11_" + brealname + "22_" + bidcard);
                    OkHttp3Utlis.getIntance().postOkhttp(ApiService.BASE_URL + "/mobile/Mymessage/kaadd", map, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if (response.isSuccessful()) {
                                String result = response.body().string().trim();
                                Logger.i("没有绑定银行身份认证：" + result);
                                Gson gson = new Gson();
                                CardWei_Info cardWei_info = gson.fromJson(result, CardWei_Info.class);
                                int code = cardWei_info.getCode();
                                if (code == 1) {
//                                    Hawk.delete("biddcard");
//                                    Hawk.delete("brealname");
                                    Hawk.put("real_name", brealname);
                                    Hawk.put("real", "1");//身份认证
                                    startAct(Card_Activity.class);

                                }

                            }

                        }
                    });
                }
                break;

        }
    }
}
